
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections;
using RSSRadio.Model;

namespace RSSRadio
{
	public class FeedViewer : CoolListView
	{
        //public delegate void FeedSelectionChangedHandler(object sender, FeedSelectionChangedArgs a);
        

        public EpisodeDetails[] Episodes; 
		public Subscription Feed;
        MenuItem mnuDownload;
        private bool canDownloadPodcast = false;


        public bool CanDownloadPodcast
        {
            get { return this.canDownloadPodcast; }
        }


		public FeedViewer()
			: base()
		{
		}

        public FeedViewer(Subscription feed)
			: base()
		{
			if (feed == null)
				throw new ArgumentNullException();

			Feed = feed;
            Episodes = Feed.episodes;


			this.KeyDown += new KeyEventHandler(FeedViewer_KeyDown);
			this.MouseDown += new MouseEventHandler(FeedViewer_MouseDown);

			// set up the tap and hold menu
			try
			{
				ContextMenu tapAndHoldMenu = new ContextMenu();
				tapAndHoldMenu.Popup += new EventHandler(tapAndHoldMenu_Popup);
				MenuItem mnuDetails = new MenuItem();
				mnuDetails.Text = "Details";
				mnuDetails.Click += new EventHandler(mnuDetails_Click);
				mnuDownload = new MenuItem();
				mnuDownload.Text = "Download";
				mnuDownload.Click += new EventHandler(mnuDownload_Click);
				tapAndHoldMenu.MenuItems.Add(mnuDetails);
				tapAndHoldMenu.MenuItems.Add(mnuDownload);
				this.ContextMenu = tapAndHoldMenu; // throws exception on smartphone
			}
			catch { }

            LoadEpisodes();
        
        }


        protected override void SelectedItemIndexChanged()
        {
            canDownloadPodcast = PodcastActions.canDownloadPodcast(Feed, Episodes[SelectedItemIndex]);
            mnuDownload.Enabled = canDownloadPodcast;

            base.SelectedItemIndexChanged();
        }
		void mnuDownload_Click(object sender, EventArgs e)
		{
			DownloadSelectedItem();
		}

		void mnuDetails_Click(object sender, EventArgs e)
		{
			ShowSelectedEpisodeDetails();
		}

		void FeedViewer_MouseDown(object sender, MouseEventArgs e)
		{
			int itemIndex = (e.Y / ItemHeightPixels) + ScrollBar.Value;
			int maxItemsInView = (Items.Count < MaxDisplayableItems) ? Items.Count : MaxDisplayableItems;
			bool clickedValidItem = (itemIndex < ScrollBar.Value + maxItemsInView);

			if (clickedValidItem)
				SelectedItemIndex = itemIndex;

			this.Refresh();

			// don't play when called from tap and hold context menu
			if (sender != null && clickedValidItem)
			//SubscriptionActions.Play(((Subscription)Globals.DownloadedSubscriptions.Subscription[this.SelectedItemIndex]).FilePath);
			{
				ShowSelectedEpisodeDetails();

				//PodcastDownloader2 downloader = new PodcastDownloader2(episode.Url, info);
				//downloader.DownloadAsync();
				//DownloadManager downloadMgr = new DownloadManager();
				//downloadMgr.ShowDialog();
			}
		}

		private void ShowSelectedEpisodeDetails()
		{
			EpisodeDetails episode = Episodes[SelectedItemIndex];

			FeedEpisodeDetailsForm episodeDetails = new FeedEpisodeDetailsForm(Feed,episode);
			episodeDetails.ShowDialog();
		}

		void FeedViewer_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Enter)
				ShowSelectedEpisodeDetails();
		}

		void tapAndHoldMenu_Popup(object sender, EventArgs e)
		{
			FeedViewer_MouseDown(null, new MouseEventArgs(MouseButtons.None, 0, 0, this.Parent.PointToClient(MousePosition).Y, 0));
		}

		internal void DownloadSelectedItem()
		{
            PodcastActions.Download(Feed, Episodes[SelectedItemIndex]);
			((Form)this.Parent).Close();
		}
		/// <summary>
		/// Fills the internal item list with feed episode info for displaying
		/// </summary>
		public void LoadEpisodes()
		{
			Items.Clear();

			foreach (EpisodeDetails details in Episodes)
			{
				CoolItem item = new CoolItem();

				if (!Globals.DownloadedPodcasts.Contains(details))
                    item.setIconResourceStrings("RSSRadio.graphics.feedEpisode.bmp",
                        "RSSRadio.graphics.feedEpisode32.bmp");
				else
                    item.setIconResourceStrings("RSSRadio.graphics.feedEpisodeGray.bmp",
                        "RSSRadio.graphics.feedEpisodeGray32.bmp");

				item.Title = details.Title;

				string textDescription = StringUtil.StripHTML((details.Description.Length > 500) ? details.Description.Substring(0, 500) : details.Description);
				textDescription = StringUtil.StripNewLines(textDescription);
				if (textDescription.Length > 100)
					textDescription = textDescription.Substring(0, 100);
				item.Description = textDescription;

				Items.Add(item);
			}
            SelectedItemIndexChanged();

		}

	}
}
